package com.example.powersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.powersystem.dao.LineMapper;
import com.example.powersystem.pojo.Line;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smd
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/line")
public class LineController {
    @Resource
    private LineMapper lineMapper;

    //添加线路
    @RequestMapping("/addLine")
    public int addLine() {
        Line line = new Line();
        line.setLineCode("222");
        line.setLineName("test2");
        line.setLineLength("100");
        line.setLoopLength("0");
        line.setTowerBase(10);
        line.setLineStart("1234567890");
        line.setLineEnd("0987654321");
        line.setPutDate(new Date());
        line.setVoltageGrade(360);
        line.setRemark("测试线路");
        line.setCreateDate(new Date());
        line.setCreateUser(2);
        return lineMapper.insert(line);
    }

    //修改线路
    @RequestMapping("/updLine")
    public int updLine() {
        Line line = new Line();
        line.setLineCode("222");
        line.setLineLength("102");
        line.setLoopLength("0");
        line.setTowerBase(20);
        return lineMapper.updateById(line);
    }

    //删除线路-逻辑删除
    @RequestMapping("/delLine")
    public int delLine() {
        Line line = new Line();
        line.setLineCode("222");
        line.setIsDeleted(0);
        return lineMapper.updateById(line);
    }

    //查询-根据线路编号和isDeleted=1查询
    @RequestMapping("/selectByCodeAndDelTest")
    public int select1Test() {
        //条件构造器：用于构建sql条件
        QueryWrapper<Line> queryWrapper = new QueryWrapper<>();
        // where line_Code = "222" and is_Deleted = 1
        queryWrapper.eq("line_Code", "222").eq("is_Deleted", 1);
        Line l = lineMapper.selectOne(queryWrapper);
        Integer num = 1;
        if (l == null) {
            num = 0;
        }
        return num;
    }
    //查询-根据线路编号和isDeleted=1查询
    @RequestMapping("/selectByCodeAndDel")
    public Line select1() {
        //条件构造器：用于构建sql条件
        QueryWrapper<Line> queryWrapper = new QueryWrapper<>();
        // where line_Code = "222" and is_Deleted = 1
        queryWrapper.eq("line_Code", "222").eq("is_Deleted", 1);
        Line l = lineMapper.selectOne(queryWrapper);
//        System.out.println("line:"+l);//不存在时结果为null
        return l;
    }
    //查询全部线路-根据isDeleted=1查询
    @RequestMapping("/selectAll")
    public List<Line> selectAll() {
        //条件构造器：用于构建sql条件
        QueryWrapper<Line> queryWrapper = new QueryWrapper<>();
        // where line_Code = "222" and is_Deleted = 1
        queryWrapper.eq("is_Deleted", 1);
       return lineMapper.selectList(queryWrapper);
    }
}
